package com.seamtop.cuber.common.util;

import com.seamtop.cuber.common.entriy.Duration;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by feng on 2015/8/8.
 */
public class DateUtil {


    private static final SimpleDateFormat sdf =   new SimpleDateFormat("yyMMdd");

    private static final SimpleDateFormat full_sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static final SimpleDateFormat tdf = new SimpleDateFormat("yyyy-MM-dd");

    public static boolean isTimeStampBeforeNow(String str){
        if(StringUtil.isEmpty(str) || !StringUtil.isDouble(str)){
            return false;
        }
        long t = Long.valueOf(str);
        if(t < new Date().getTime()){
            return true;
        }else{
            return false;
        }
    }

    public static long getTimeStamp(Date date){
        if(date == null){
            return 0;
        }else{
            return date.getTime();
        }
    }

    /**
     * 获取指定日期是周几
     * @param date
     * @return
     */
    public static int getDayOfWeek(Date date){
        Calendar cal=Calendar.getInstance();
        cal.setTime(date);
        int weekDay = cal.get(Calendar.DAY_OF_WEEK)-1;
        return weekDay;
    }

    public static int getDayOfMonth(Date date){
        Calendar cal=Calendar.getInstance();
        cal.setTime(date);
        int monthDay = cal.get(Calendar.DAY_OF_MONTH);
        return monthDay;
    }


    public static Duration getDurationByStatModel(Date date,String statModel){
        Duration duration = new Duration();
        if("day".equals(statModel)){//昨天
            long lastDayTimeStamp = getAheadDayTimeStamp(date,-1);
            long dayTimeStamp = getAheadDayTimeStamp(date,0);
            duration.setStartTime(lastDayTimeStamp);
            duration.setEndTime(dayTimeStamp);
        }else if("week".equals(statModel)){//上周
            long lastDayTimeStamp = getAheadWeekTimeStamp(date,-1);
            long dayTimeStamp = getAheadWeekTimeStamp(date,0);
            duration.setStartTime(lastDayTimeStamp);
            duration.setEndTime(dayTimeStamp);
        }else if("month".equals(statModel)){//上个月
            long lastDayTimeStamp = getAheadMonthTimeStamp(date,-1);
            long dayTimeStamp = getAheadMonthTimeStamp(date,0);
            duration.setStartTime(lastDayTimeStamp);
            duration.setEndTime(dayTimeStamp);
        }else if("recently-week".equals(statModel)){//最近一周
            long lastDayTimeStamp = getAheadRecentyDayTimeStamp(date, -7);
            long dayTimeStamp = getAheadRecentyDayTimeStamp(date, 0);
            duration.setStartTime(lastDayTimeStamp);
            duration.setEndTime(dayTimeStamp);
        }else if("recently-month".equals(statModel)){
            long lastDayTimeStamp = getAheadRecentyDayTimeStamp(date, -30);
            long dayTimeStamp = getAheadRecentyDayTimeStamp(date, 0);
            duration.setStartTime(lastDayTimeStamp);
            duration.setEndTime(dayTimeStamp);
        }
        return duration;
    }

    //获得月初时间戳
    public static long getAheadMonthTimeStamp(int ahreadMonth) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.add(Calendar.MONTH, ahreadMonth);
        calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return (long) (calendar.getTimeInMillis());
    }


    //获得月初时间戳
    public static long getAheadMonthTimeStamp(Date date,int ahreadMonth) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, ahreadMonth);
        calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return (long) (calendar.getTimeInMillis());
    }

    //获得上周一时间戳
    public static long getAheadWeekTimeStamp(int ahreadWeek) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.add(Calendar.DATE, ahreadWeek*7);
        calendar.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return (long) (calendar.getTimeInMillis());
    }

    //获得上周一时间戳
    public static long getAheadWeekTimeStamp(Date date,int ahreadWeek) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.setTime(date);
        calendar.add(Calendar.DATE, ahreadWeek*7);
        calendar.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return (long) (calendar.getTimeInMillis());
    }

    public static long getAheadDayTimeStamp(int ahead){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, ahead); //得到前一天
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return (long) (calendar.getTimeInMillis());
    }

    public static long getAheadHourTimeStamp(int ahead){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR, ahead); //得到前一天
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return (long) (calendar.getTimeInMillis());
    }

    public static long getAheadMinuteTimeStamp(int ahead){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, ahead); //得到前一天
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return (long) (calendar.getTimeInMillis());
    }

    public static long getAheadDayTimeStamp(Date date,int ahead){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, ahead); //得到前一天
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return (long) (calendar.getTimeInMillis());
    }



    public static long getAheadRecentyDayTimeStamp(Date date,int ahead){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 0 - ahead);
        return (long) (calendar.getTimeInMillis());
    }

    public static long getAheadRecentyHourTimeStamp(Date date,int ahead){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, 0 - ahead);
        return (long) (calendar.getTimeInMillis());
    }


    public static long getAheadRecentyMinuteTimeStamp(Date date,int ahead){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, 0 - ahead);
        return (long) (calendar.getTimeInMillis());
    }

    public static String getDateStrOfTimeStamp(long t){
        String dateStr = full_sdf.format(new Date(t));
        return dateStr;
    }

    public static String getSimpleStrOfTimeStamp(long t){
        String dateStr = sdf.format(new Date(t));
        return dateStr;
    }

    public static String getDateStr(Date date){
        String str = sdf.format(date);
        return str;
    }

    public static String getFullDateStr(Date date){
        String str = full_sdf.format(date);
        return str;
    }

    public static Date transStrToDate(String str){
        Date date = null;
        try{
            date = tdf.parse(str);
        }catch (Exception e){
            e.printStackTrace();
        }
        return date;
    }

    public static Date getNextDate(String str) throws Exception{
        Date date = tdf.parse(str);
        long t = getAheadDayTimeStamp(date,1);
        Date d2 = new Date(t);
        System.out.println(d2);
        return d2;
    }


    public static String getDurationDateStrOfDay(long t){

        Date date = new Date(t);
        String str = sdf.format(date);
        return str;
    }


    public static String combineBaseKeyOfDay(Duration duration){
        long startTime = duration.getStartTime();
        long endTime = duration.getEndTime();
        String startDay = getDurationDateStrOfDay(startTime);
        String result = startDay ;
        return result;
    }


    public static void main(String [] args) throws Exception{
//        Date date = new Date();
//        long t= getAheadDayTimeStamp(date,-1);
//        String dateStr = getDurationDateStrOfDay(t);
//        System.out.println(dateStr);
        getNextDate("2015-08-02");

    }
}
